<?php

namespace App\Http\Controllers\Api\Quiz;

use App\Http\Responses\RespondsJson;
use App\Models\Issue\OptionService;
use App\Models\Quiz\RecordService;
use Exception;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RecordController extends Controller
{
    use RespondsJson;

    protected $record;
    protected $option;

    public function __construct(
        RecordService $record,
        OptionService $option
    ) {
        $this->record = $record;
        $this->option = $option;
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'user_id' => 'required',
            'activity_id' => 'required',
            'issue_id' => 'required',
            'option_id' => 'required',
        ]);

        /*
        if (null === ($option = $this->option->getById($request->input('option_id')))) {
            throw new Exception('答案不存在');
        }

        if ($request->input('issue_id') != $option->issue_id) {
            throw new Exception('答案和问题不匹配');
        }
        */

        if ($this->record->createAndGetId($request->all())) {
            return $this->respondJson('竞猜成功');
        }

        return $this->respondJson('竞猜失败', 406);
    }
}
